package cn.nawang.ebeim.api.repository.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.domain.StorageCloud;
import cn.nawang.ebeim.api.utils.ConvertUtil;

/**
 * @ClassName: StorageCloudRepositoryImpl
 * @Description: TODO
 * @author fanyb
 * @date Aug 18, 2015 9:13:18 AM
 * @version v1.0
 */
@Repository
public class StorageCloudRepositoryImpl extends BaseQuery{
  
  @Autowired
  private EntityManagerFactory entityManagerFactory;
  
  public List<StorageCloud> findStorageCloudById(String param){
    List objList = null;
    String sql = "select sc from StorageCloud sc where sc.id in "+param;
    EntityManager em = entityManagerFactory.createEntityManager();
    try{
      Query query =  em.createQuery(sql);
      objList = query.getResultList();
    }catch(Exception ex){
      ex.printStackTrace();
    }finally{
      em.close();
    }
    return objList;
  }
  
  public List<StorageCloud> findByParam(String property,String value){
    List<StorageCloud> objList = null;
    String sql="select sc from StorageCloud sc where sc."+property+" =:value";
    try{
      Map<String,Object> paraMap = new HashMap<String,Object>();
      paraMap.put("value", value);
      objList = queryObject(sql,paraMap);
    }catch(Exception ex){
      ex.printStackTrace();
    }
    return objList;
  }
  
  public PageList findAllByPaging(Pages page) {
    String hqlCount = "select count(sc.id) from StorageCloud sc where 1=1 ";
    String hqlList="from StorageCloud sc where 1=1 ";
    String order=" order by sc.id desc";
    PageList pl = null;
    try{
      String cod = joinSearchCondition(page);//拼接查询条件
      pl = queryListByPaging(hqlCount+cod+order, hqlList+cod+order, page);
    }catch (Exception ex) {
      ex.printStackTrace();
    }
    return pl;
  }
  
  public boolean delete(String[] ids){
    try{
      String values=ConvertUtil.toDbString(ids);
      String hqlList="delete from StorageCloud where id in"+values;
      Map<String, Object> paraMap = new HashMap<String, Object>();
      boolean deleteFlag = this.execHQL(hqlList, paraMap);
      return deleteFlag;
    }catch(Exception e){
      e.printStackTrace();
    }
    return false;
  }
}
